package com.centaline.androidsalesblog.widgets;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.DrawableRes;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.baidu.mobstat.StatService;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.ShareActivity;
import com.centaline.androidsalesblog.adapter.ShareAdapter;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.businissobject.ShareBo;

import java.util.Locale;

/**
 * 分享对话框
 * Created by guilin on 16/5/9.
 */
public class ShareDialog {

    private final Context context;

    private View view;
    private RecyclerView rv_share;
    protected BottomSheetDialog bottomSheetDialog;

    public ShareDialog(final Context context, final ShareBo shareBo) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.layout_share, null);
        rv_share = (RecyclerView) view.findViewById(R.id.rv_share);

        rv_share.setLayoutManager(new GridLayoutManager(context, 3));
        rv_share.setAdapter(new ShareAdapter(context, getTitles(), getImageIds(),
                new RecyclerViewItemOnClickListener<Integer>() {
                    @Override
                    public void onItemOnClick(Integer integer, int tag) {
                        switch (integer) {
                            case 0://微博
                            case 1://微信
                            case 2:
                                context.startActivity(new Intent(context, ShareActivity.class)
                                        .putExtra(ExtraConstant.ShareType, integer)
                                        .putExtra(ExtraConstant.ShareContent, shareBo));
                                StatService.onEvent(context,
                                        "C5_2_Type",
                                        String.format(Locale.CHINA, "%s_%s",
                                                AppConstant.CityCode,
                                                integer == 0 ? "微博" : "微信"));
                                break;
                            default:
                                break;
                        }
                        bottomSheetDialog.dismiss();
                    }
                }));
    }

    protected String[] getTitles() {
        return new String[]{"微博", "微信", "朋友圈"};
    }

    @DrawableRes
    protected int[] getImageIds() {
        return new int[]{R.drawable.ic_share_sina, R.drawable.ic_share_wechat, R.drawable.ic_share_wxcircle};
    }

    public void show() {
        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(context);
            bottomSheetDialog.setContentView(view);
        }
        bottomSheetDialog.show();
    }
}
